$(document).ready(function(){
	
	
//搜索选择框
	var showsearch = false;
	$("#nowselect").click(function(){
		$("#nowselect").addClass("hidden");
		$(".jsearch").removeClass("hidden");
	})
	
	$(".jsearch").click(function(){
		$("#nowselect").text(this.innerText);
		$("#sousuo").attr("href","?h=search&type="+$(this).attr("role")+"&content=");
		$(".jsearch").addClass("hidden");
		$("#nowselect").removeClass("hidden");
	})
	
	$("#sousuo").click(function(){
		
		var str=$("#sousuo").attr("href");
		var str2=$.trim($("#content").val());
		str2= html_encode(str2); 
		$("#sousuo").attr("href",str+str2+"&p=1");
		$("#content").val("");
	})
	
	$("#content").keydown(function(event){ 
    if(event.which=="13"){

		var str=$("#sousuo").attr("href");
	
		var str2=$.trim($("#content").val());
		str2= html_encode(str2); 
		$("#sousuo").attr("href",str+str2+"&p=1");
		$("#content").val("");
		document.getElementById("sousuo").click(); 
	}
  });
//判断是否已经登录
	var islogin = function(){
		$.ajax({
			type:"get",
			url:"userWeb/checkLogin",
			async:false,
			success: function(res){
				if (res.code==1) {
					t = false;
				}else if (res.code==0){
					t = true;
				}
			}
		})
	    return t;
	}
//首页右上角个人菜单
	var showperson = false;
	$("#photo").click(function(){
		showperson = !showperson;
		if(showperson){
			$("#personmenu").removeClass("hidden");
		}else{
			$("#personmenu").addClass("hidden");
		}
		
	})
	$("#photo").mouseenter(function(){
	    $("#personmenu").removeClass("hidden");
	  });
	$("#photo").mouseleave(function(){
	    $("#personmenu").addClass("hidden");
	  });
	$("#personmenu").mouseenter(function(){
	    $("#personmenu").removeClass("hidden");
	  });
	$("#personmenu").mouseleave(function(){
	    $("#personmenu").addClass("hidden");
	  });
//首页导航栏选中变色
	function getUrlParam(name)
	{
		var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
		var r = window.location.search.substr(1).match(reg);  //匹配目标参数
		if (r!=null) return unescape(r[2]); return null; //返回参数值
	} 
	var urlh = getUrlParam('h');
	if(urlh!==null||!urlh=="null"){
		urlh = urlh.substring(0,4);
	}
	if (urlh==null||urlh=="null") {
		$(".j-nav").css("background-color","#transparent");
		$(".j-home").css({"background-color":"#d72323","color":"#fff"});
	}else if(urlh=="live"){
		$(".j-nav").css("background-color","#transparent");
		$(".j-live-list").css({"background-color":"#d72323","color":"#fff"});
	}else if(urlh=="cour"){
		$(".j-nav").css("background-color","#transparent");
		$(".j-course").css({"background-color":"#d72323","color":"#fff"});
	}else if(urlh=="syst"){
		$(".j-nav").css("background-color","#transparent");
		$(".j-system").css({"background-color":"#d72323","color":"#fff"});
	}
//个人主页页面显示列表切换
	$(".j-student").click(function(){
		$(".j-student").removeClass("te");
		$(this).addClass("te");
		var showindex = $(".j-student").index($(this));
		$(".j-stumoudle").addClass("hidden");
		$(".j-stumoudle").eq(showindex).removeClass("hidden");
	})
	var urlsta = getUrlParam('sta');
	$(".j-student").eq(urlsta).trigger("click");
//查看留言回复
	$(".j-reply").click(function() {
		var replyin = $(this).parent().parent().parent().siblings().last();
		replyin.removeClass("hidden");
		replyin.children().first().focus();
		var t = $(window).scrollTop();
	    $('body,html').animate({'scrollTop':t+200},500)
	})
	$(".j-newreply").click(function(){
		var textbox = $(this).parent().prev();
		var newtext = $.trim(textbox.val());
		if (!newtext){
			layer.msg("请输入回复内容！",{icon: 2},{time:1000});
			return false;
		}else{
			newtext= html_encode(newtext);
		}
		var replybox = $(this).parent().parent()
		var pid = $(this).attr("pid");
		var id = $("#id").val();
		var uimg = $("#uimg").val();
		var uname = $("#uname").val();
		var teacher_id = $("#teacher_id").val();
		$.post("teacher/LeaveMessage",{
			"message.content" : newtext,
			"message.teacher_id" : teacher_id,
			"message.id": pid
		},function(res){
			if(res.code==0){
				replybox.addClass("hidden");
				textbox.val("");
				var photo=uimg;
				var name =uname;
				var time = res.content.create_time;
				var newreply = '<div class="Message-reply-down"><img src="'+photo+'" width="45" height="45" class="img-circle fl"><div class="Message-reply-down-p fl"><p class="color-red">'+name+'</p> <p class=" color-8080" style="word-break:break-all;">'+newtext+'</p><p class="color-8080">'+time+'</p></div></div>';
				replybox.before(newreply);
				var t = $(window).scrollTop();
			    $('body,html').animate({'scrollTop':t-200},500);
			}else{
				layer.msg("网络不给力哦，请尝试重新提交",{icon: 2},{time:1500});
			}
		})
	})
	$(".j-isreply").click(function(){
		var isreply = $(this);
		var allreply = $(this).parent().parent().parent().parent().find(".j-allreply");
		console.log($(".j-isreply").html());
		if(isreply.html()=="收起回复"){
			isreply.html("展开回复");
			allreply.hide();
		}else if (isreply.html()=="展开回复"){
			console.log("show");
			isreply.html("收起回复");
			allreply.show();
		};
	})
function html_encode(str){   
  var s = "";   
  if (str.length == 0) return "";   
  s = str.replace(/&/g, "&gt;");   
  s = s.replace(/</g, "&lt;");   
  s = s.replace(/>/g, "&gt;");   
  s = s.replace(/ /g, "&nbsp;");   
  s = s.replace(/\'/g, "&#39;");   
  s = s.replace(/\"/g, "&quot;");   
  s = s.replace(/\n/g, "<br>");   
  s = s.replace(/\t/g, "<br>"); 
  return s;   
} 
//课程评论
	$(".j-newcomment").click(function() {
		if(islogin()){
			var textbox = $(this).parent().find("textarea");
			var newtext = $.trim($(this).parent().find("textarea").val());
			if (!newtext){
				layer.msg("请输入内容",{icon:2},{time:1500});
				return;
			}else{
				newtext= html_encode(newtext);
			}
			var courseid =  getUrlParam("id");
			$.post("commentWeb/addCouComment",{
				"comment.course_id":courseid,
				"comment.content":newtext
			},function(res){
				if(res.code==0){
					var commendbox = $(".j-commendbox");
					var photo=res.content.user.img;
					var name = res.content.user.name;
					var time = res.content.commments.create_time;
					newtext = res.content.commments.content; 
					var newcommend = '<li><img src="'+photo+'" class="img-circle fl"  width="60" height="60"><p class="fl te"><span class=" color-8080">'+name+'</span><span class=" color-333" style="word-break:break-all;">'+newtext+'</span><span class="color-8080 m-com-mart15">时间：'+time+'</span></p></li>';
					commendbox.after(newcommend);
					textbox.val("");
					var t = $(window).scrollTop();
					$('body,html').animate({'scrollTop':t+200},500)
				}
			})
		}else{
			layer.msg("请先登录",{icon: 2});
		}
	})
//课时评论
	var textbox = $(".j-newkspl").parent().find("textarea");
	textbox.keyup(function(){
			var length = $(this).val().length;
			if(length<=300){
				$(this).parent().find(".zs").html(length+"/300");
			}
		})
	$(".j-newkspl").click(function() {
		if(islogin()){
			var textbox = $(this).parent().find("textarea");
			var newtext = $.trim($(this).parent().find("textarea").val());
			if (!newtext){
				layer.msg("请输入内容",{icon: 2},{time:1500});
				return;
			}else{
				newtext = html_encode(newtext);
			}
			var courseid =  getUrlParam("id");
			var liveid = getUrlParam("l_id");
			$.post("commentWeb/addCouComment",{
				"comment.course_id":courseid,
				"comment.content":newtext,
				"comment.lesson_id":liveid
			},function(res){
				console.log(res);
				if(res.code==0){
					var commendbox = $(".newksplbox");
					var photo=res.content.user.img;
					var name = res.content.user.name;
					var time = res.content.commments.create_time;
					newtext = res.content.commments.content; 
					var newcommend = '<li><img src="'+photo+'" class="img-circle fl"  width="60" height="60"><p class="fl te"><span class=" color-8080">'+name+'</span><span class=" color-333" style="word-break:break-all;">'+newtext+'</span><span class="color-8080 m-com-mart15">时间：'+time+'</span></p></li>';
					commendbox.after(newcommend);
					textbox.val("");
					var t = $(window).scrollTop();
					$('body,html').animate({'scrollTop':t+200},500)
				}
			})
		}else{
			layer.msg("请先登录",{icon: 2});
		}
	})
//课程详情页目录评论切换
	$(".j-courseDetailssel").click(function(){
		$(".j-courseDetailssel").removeClass("te");
		$(this).addClass("te");
		var showindex = $(".j-courseDetailssel").index($(this));
		$(".j-courseDetailmoudle").addClass("hidden");
		$(".j-courseDetailmoudle").eq(showindex).removeClass("hidden");
	})
//用户个人主页更换头像
	layer.closeAll();
	$(".j-student-photo").click(function(){
		layer.open({
			  type: 2,
			  title:false,
			  closeBtn: 0,
			  scrollbar: false,
			  area: ['700px', '550px'], //宽高
			  content: 'userWeb/uploadPhoto'
			});
	})
//讲师详情页关注
	var tficon = function(){
		var urltid = getUrlParam('id');
		var on = function(){
			$.post("teacher/focusTeacher",{
				teacher_id:urltid
			},function(res){
				console.log(res);
				if(res.content=="0"){
					$(".j-tficon").html("关注");
					$(".j-tficon").css({"background-color":"#fff","color":"#808080"});	
				}else if(res.content=="1"){
					$(".j-tficon").text("已关注");
					$(".j-tficon").css({"background-color":"#d72323","color":"#fff"});
				}
			})
		};
		if($(".j-tficon").html()=="关注"){
			on();
		}else if($(".j-tficon").html()=="已关注"){  
		  layer.confirm('确认要取消关注吗？', {
			  title: false,
			  closeBtn: 0,
			  skin: 'demo-class',
			  btn: ['确定', '取消']
			}, function(){
			  on();
			  layer.closeAll();
			}, function(){
			 
			});
		}
	}	
	
	$(".j-tficon").click(function(){
		$.get("userWeb/checkLogin",function(res){
				if (res.code==1) {
					layer.msg("请先登录",{icon: 2},{time:1500});
				}else if (res.code==0){
					tficon();
				}
			});
	})
//关注讲师页面取消关注
	$(".j-notficon").click(function(e){
		var thisbtn = $(this);
		var tid = $(this).attr("name");
		var tidbox = "#"+tid;
		
	    var nowindow = layer.confirm('确认要取消关注吗？', {
			  title: false,
			  closeBtn: 0,
			  skin: 'demo-class',
			  btn: ['确定', '取消']
			}, function(){ 
				$.post("teacher/focusTeacher",{
					teacher_id: tid
				},function(res){
					if (res.content=="0") {
						thisbtn.text("关注");
						$(".j-gzjs").remove(tidbox);
						layer.close(nowindow);
					}else{
						layer.close(nowindow);
						layer.msg("网络不给力哦，请再试一次",{icon: 2},{time:1500});
					}
				})
			}, function(){
				layer.closeAll();
			});
	})
//用户向讲师留言
	function leaveinfo() {
		layer.open({
			  type: 1,
			  title:"留言",
			  closeBtn: 1, //不显示关闭按钮
			  area: ['440px', '200px'],
			  anim: 2,
			  content: '<div class="fl" style="margin:0 20px;width:400px;"><textarea class="j-leavetext" style="min-height:100px;margin-bottom:10px;" maxlength=300></textarea><span class="zs j-zs">0/300</span><button class="fr button-red line-hg-30 j-newleavemsg">发表留言</button></div>'
			});
		$(".j-newleavemsg").click(function(){
			var urltid = getUrlParam('id');
			var content = $(".j-leavetext").val();
			var val = $.trim($(".j-leavetext").val());
			if(val==""||val==undefined){
				layer.msg("请输入留言",{icon: 2},{time:1500});
			}else{
				content= html_encode(val);//替换html,js标签
				$.post("teacher/LeaveMessage",{
					"message.content" : content,
					"message.teacher_id" : urltid
				},function(res){
					if(res.code==0){
						layer.closeAll();
						layer.msg("留言成功",{icon: 1},{time:1500});
					}else{
						layer.msg("网络不给力哦，请尝试重新提交",{icon: 2},{time:1500});
					}
				})
			}
		})
		$(".j-leavetext").keyup(function(){
			var length = $(".j-leavetext").val().length;
			if(length<=300){
				$(".j-zs").html(length+"/300");
			}
		})
	}
	$(".j-leavemesg").click(function(){
		$.get("userWeb/checkLogin",function(res){
			if (res.code==1) {
				layer.msg("请先登录",{icon: 2},{time:1500});
			}else if (res.code==0){
				leaveinfo();
			}
		});	
	})
//直播页面右侧评论
	$(".j-livesend").click(function(){
		if(!islogin()){
			layer.msg("请先登录",{icon: 2},{time:1500});
			return;
		}
		var commenttext = $.trim($(".j-livecomment").val());
		var liveid =  getUrlParam('id');
		if (!commenttext){
			$("#tishi").removeClass("hide");
			//layer.msg("请输入内容",{icon: 2},{time:1500});
			setTimeout(function(){
				$("#tishi").addClass("hide");
			}, 1500)
			return;
		}else{
			commenttext = html_encode(commenttext);
		} 
		 
		$.post("commentWeb/addLiveComment",{
			"comment.live_id":liveid,
			"comment.content":commenttext
		},function(res){
			if (res.code=="0") {
				/*var newcom = '<li><img src="'+res.content.user.img+'"class="img-circle fl" ><p class="fl"><span class=" color-8080">'+res.content.user.name+'</span><span class=" color-333" style="width: 175px;">'+commenttext+'</span></p></li>';
				$(".j-livecombox").append(newcom);*/
				layer.msg("发送成功！",{icon: 1},{time:700});
				$(".j-livecomment").val("");
				$('.j-livecombox').scrollTop( $('.j-livecombox')[0].scrollHeight );
			}else if (res.code=="1") {
				layer.msg("请先登录",{icon: 2},{time:1500});
			}
		})
	})
	
//公告折叠
	var shownotice = false;
	$(".j-zhedie").click(function(){
		console.log($(this).parent().parent().find(".j-zhe"));
		shownotice = !shownotice;
		if(shownotice==true){
			$(this).parent().find(".j-noticea").eq(0).hide();
			$(this).parent().find(".j-noticeb").eq(0).show();
			$(this).parent().parent().find(".j-zhe").eq(0).attr("src","assets/front/images/top-jt.png");
		}else{
			$(this).parent().find(".j-noticea").eq(0).show();
			$(this).parent().find(".j-noticeb").eq(0).hide();
			$(this).parent().parent().find(".j-zhe").eq(0).attr("src","assets/front/images/right-jt.png");
		}
	})
//讲师风采列表--关注或取消关注讲师
	$(".j-focus").click(function(){
		var tid = $(this).attr("id");
		$.get("userWeb/checkLogin",function(res){
				if (res.code==1) {
					layer.msg("请先登录",{icon: 2},{time:1500});
				}else if (res.code==0){
					if($("#"+tid).text()=="关注")	{
						$.post("teacher/focusTeacher",{ teacher_id:tid},function(data){
							if(data.code ==0){
								$("#"+tid).text("已关注")
								$("#"+tid).attr("style","width:64px;overflow:hidden;background-color:#d72323;color:#fff");
								/*$("#"+tid).removeClass("j-focus");
								$("#"+tid).addClass("j-notfocused");*/
							}
						});
					}else{
						layer.confirm('确认要取消关注吗？',{
							  title: false,
							  closeBtn: 0,
							  skin: 'demo-class',
							  btn: ['确定', '取消']
							}, function(){ 
							$.post("teacher/focusTeacher",{ teacher_id:tid},function(data){
								if(data.code ==0){
									$("#"+tid).text("关注");
									$("#"+tid).attr("style","");
									$("#"+tid).removeClass("j-notfocused");
									$("#"+tid).addClass("j-focus");
									layer.closeAll();
								}
							});
						});
					}
				}
		});
	})
});